package com.java.interview.i线程安全;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.CountDownLatch;

/**
 * @Description: ArrayList HashMap StringBuilder
 * @Auther: sixbing  QQ:414409451
 * @Date: 2018/12/10 10:08
 */
public class Test {
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            try {
                test();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

    }

    public static void test() {
        // 用来测试的List
//        List<Object> list = new ArrayList<Object>();//不安全
        List<Object> list = new Vector<>();//安全的
        // 线程数量(1000)
        int threadCount = 1000;

        // 用来让主线程等待threadCount个子线程执行完毕
        CountDownLatch countDownLatch = new CountDownLatch(threadCount);

        // 启动threadCount个子线程
        for (int i = 0; i < threadCount; i++) {
            Thread thread = new Thread(new MyThread(list, countDownLatch));
            thread.start();
        }

        try {
            // 主线程等待所有子线程执行完成，再向下执行
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // List的size
        System.out.println(list.size());
    }

    static class MyThread implements Runnable {
        private List<Object> list;

        private CountDownLatch countDownLatch;

        public MyThread(List<Object> list, CountDownLatch countDownLatch) {
            this.list = list;
            this.countDownLatch = countDownLatch;
        }

        public void run() {
            // 每个线程向List中添加100个元素
            for (int i = 0; i < 100; i++) {
                list.add(new Object());
            }

            // 完成一个子线程
            countDownLatch.countDown();
        }
    }
}
